package struct
{
	public class List
	{
		private var _head:Node;
		private var _tail:Node;
		public var size:int = 0;
		public var currentNode:Node;
		
		public function List()
		{
			currentNode = _head = _tail = new Node(null);
		}
		
		public function toHead():void{
			currentNode = head;
		}
		
		public function toTail():void{
			currentNode = _tail;
		}
		
		public function current():Object{
//			if(currentNode == head){
//				currentNode = head.next;
//			}
			return currentNode.content;
		}
		
		public function add(content:Object):void{
			var node:Node = new Node(content);
			_tail.next = node;
			node.pre = _tail;
			_tail = node;
			size++;
		}
		
		public function remove(content:Object):Node {
			var t:Node = _head;
			while(t.next != null){
				t = t.next;
				if(t.content == content){
					removeNode(t);
					size--;
					return t;
				}
			}
			return null;
		}
		
		public function removeNode(node:Node):void{
			if (node.next != null) 
				node.next.pre = node.pre;
			else 
				_tail = _tail.pre;
			node.pre.next = node.next;
			node.next = null;
			node.pre = null;
		}
		
		public function removeCurrent():void{
			if(_head == currentNode) return;
			currentNode.pre.next = currentNode.next;
			currentNode.next.pre = currentNode.pre;
			currentNode.next = null;
			currentNode.pre = null;
		}
		
		public function hasNext():Boolean{
			if(currentNode.next == null) return false;
			return true;
		}
		
		public function hasPre():Boolean{
			if (currentNode.pre == _head)
				return false;
			return true;
		}
		
		public function next():Object{
			if(currentNode.next != null){
				currentNode = currentNode.next;
				return currentNode.content;
			}
			return null;
		}
		
		public function pre():Object{
			if(currentNode.pre != null){
				currentNode = currentNode.pre;
				return currentNode.content;
			}
			return null;	
		}

		public function get head():Node
		{
			return _head.next;
		}
	}
}